﻿#include "ssOBB2d.h"
#include "./ss3dMathFunction.h"

namespace StarSeeker
{

ssOBB2d::ssOBB2d()
{
}

ssOBB2d::~ssOBB2d()
{
}

void ssOBB2d::InitByCenter(ssfloat centerX, ssfloat centerY, ssfloat xAxisX, ssfloat xAxisY, ssfloat yAxisX, ssfloat yAxisY, ssfloat xLen, ssfloat ylen)
{
	m_center.Set(centerX, centerY);
	m_xAxis.Set(xAxisX, xAxisY);
	m_yAxis.Set(yAxisX, yAxisY);
	m_len.Set(xLen, ylen);
}

void ssOBB2d::InitByCenter(const ssVector2& center, const ssVector2& xAxis, const ssVector2& yAxis, ssfloat xLen, ssfloat ylen)
{
	m_center = center;
	m_xAxis = xAxis;
	m_yAxis = yAxis;
	m_len.Set(xLen, ylen);
}

void ssOBB2d::InitByCenter(ssfloat centerX, ssfloat centerY, ssfloat yAxisX, ssfloat yAxisY, ssfloat xLen, ssfloat ylen)
{
	m_center.Set(centerX, centerY);
	m_yAxis.Set(yAxisX, yAxisY);
	m_len.Set(xLen, ylen);
	m_xAxis = Math3D::Rotate2dVector(m_yAxis, Math3D::gc_HALF_PI);
}

void ssOBB2d::InitByCenter(const ssVector2& center, const ssVector2& yAxis, ssfloat xLen, ssfloat ylen)
{
	m_center = center;
	m_yAxis = yAxis;
	m_len.Set(xLen, ylen);
	m_xAxis = Math3D::Rotate2dVector(m_yAxis, Math3D::gc_HALF_PI);
}

bool ssOBB2d::PointInBox(ssfloat x, ssfloat y)
{
	ssVector2 m(x, y);
	ssfloat xlen = Math3D::Point2LineDistance2D(m, m_center, m_xAxis);
	ssfloat ylen = Math3D::Point2LineDistance2D(m, m_center, m_yAxis);
	return (xlen < m_len.y*m_len.y && ylen < m_len.x*m_len.x);
}

}
